package com.student.xujiang.mysmalluse.companylogin.model;

import com.student.xujiang.mysmalluse.companylogin.retrofit2protogenesis.UserService;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

/**
 * Created by xujiang on 2017/3/22.
 */

public class OkHttp3Utils {

    private final String    TAG  = OkHttp3Utils.class.getSimpleName();
    public final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private static OkHttpClient httpClient =  new OkHttpClient.Builder()
            .addInterceptor(new Interceptor() {  //添加一个拦截器
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request.Builder builder = chain.request().newBuilder();
                   // builder.addHeader("application/json","charset=utf-8");
                    return chain.proceed(builder.build());
                }
            })
            .connectTimeout(30, TimeUnit.SECONDS) //其中connectTimeout是连接超时时间，在指定时间内还没有连接到服务器就会报SocketTimeout异常，
            .readTimeout(30,TimeUnit.SECONDS)  //而readTimeout是读取超时时间，是连接后在指定时间还没有获取到数据就超时。
            .build();

    private static UserService retrofitService = new Retrofit.Builder()
            .baseUrl(Urls.BaseUrl)
            .addConverterFactory(JacksonConverterFactory.create()) //addConverterFactory是指定使用Jackson来解析Json数据,转换器
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) //而addCallAdapterFactory，通过这个转换，才能将服务器的返回值从Retrofit默认的Call变为Observable
            .client(httpClient)
            .build()
            .create(UserService.class);

    public static UserService getInstance(){
        return retrofitService;
    }



}
